package ext.generic.wfaugment;

import java.net.URL;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;

import wt.fc.Persistable;
import wt.fc.ReferenceFactory;
import wt.fc.WTObject;

import wt.httpgw.URLFactory;

import wt.org.WTPrincipal;

import wt.util.WTException;
import wt.util.WTProperties;

import wt.workflow.definer.WfAssignedActivityTemplate;
import wt.workflow.engine.WfActivity;
import wt.workflow.engine.WfProcess;
import wt.workflow.work.WorkItem;

import com.ptc.jca.mvc.components.JcaComponentParams;
import com.ptc.mvc.components.AbstractComponentBuilder;
import com.ptc.mvc.components.ColumnConfig;
import com.ptc.mvc.components.ComponentBuilder;
import com.ptc.mvc.components.ComponentConfig;
import com.ptc.mvc.components.ComponentParams;
import com.ptc.mvc.components.TableConfig;
import com.ptc.mvc.util.ClientMessageSource;
import com.ptc.netmarkets.model.NmOid;
import com.ptc.netmarkets.util.beans.NmCommandBean;
import com.ptc.netmarkets.util.beans.NmHelperBean;
import com.ptc.netmarkets.util.beans.NmURLFactoryBean;

@ComponentBuilder("generic.addParticipantsBySelect")
public class WorkflowAddParticipantsBySelectTableBuilder extends AbstractComponentBuilder
{

    private static boolean VERBOSE = false;
    public static WTProperties wtProperties;
    private final ClientMessageSource messageSource = getMessageSource("ext.generic.wfaugment.workflowStepRB");
    static
    {
        try
        {
            wtProperties = WTProperties.getLocalProperties();
            VERBOSE = wtProperties.getProperty("ext.generic.wfaugment.verbose", false);
        }
        catch (Throwable throwable)
        {
            throw new ExceptionInInitializerError(throwable);
        }
    }

    public WorkflowAddParticipantsBySelectTableBuilder()
    {
    }

    public ArrayList buildComponentData(ComponentConfig componentconfig, ComponentParams componentparams) throws WTException
    {
        NmHelperBean nmhelperbean = ((JcaComponentParams) componentparams).getHelperBean();
        NmCommandBean nmcommandbean = nmhelperbean.getNmCommandBean();
        ArrayList arraylist = new ArrayList();
        NmOid nmoid = nmcommandbean.getActionOid();
        if (nmoid == null)
            nmoid = nmcommandbean.getPrimaryOid();
        if (VERBOSE)
            System.out.println(">>> WorkflowAddParticipantsBySelectTableBuilder nmoid  " + nmoid);
        WTObject contextObj = (WTObject) nmoid.getWtRef().getObject();
        if (contextObj instanceof WorkItem)
        {
            WorkItem workItem = (WorkItem) contextObj;
            Persistable persistable = workItem.getPrimaryBusinessObject().getObject();
            WfActivity wfactivity = (WfActivity) workItem.getSource().getObject();
            WfProcess wfprocess = wfactivity.getParentProcess();
            Hashtable activehtInguide = WorkFlowStepUtil.getWorkFlowStepInReviewGuideByName(persistable, wfprocess);
            if (VERBOSE)
            {
                System.out.println(">>> WorkflowAddParticipantsBySelectTableBuilder activehtInguide == " + activehtInguide);
            }
            //读取其父页面的完整地址，并且解析出其activeoid值
            String referer = (String) nmcommandbean.getRequestData().getCgiData().get("cgi.http_referer");
            if (VERBOSE)
                System.out.println(">>> WorkflowAddParticipantsBySelectTableBuilder cgi.http_referer == " + referer);
            URLFactory urlfactory = new URLFactory();
            NmURLFactoryBean nmurlfactorybean = new NmURLFactoryBean();
            ReferenceFactory referencefactory = new ReferenceFactory();
            URL url = urlfactory.getURL(referer);
            String qstr = url.getQuery();
            HashMap qmp = nmurlfactorybean.parseTheQueryString(qstr);
            String aciveoid = (String) qmp.get("activeoid");
            if (VERBOSE)
                System.out.println(">>> WorkflowAddParticipantsBySelectTableBuilder aciveoid == " + aciveoid);
            //activeoid查找工作模板
            WfAssignedActivityTemplate wfatp = (WfAssignedActivityTemplate) referencefactory.getReference(aciveoid).getObject();
            String rolestr = (String) activehtInguide.get(wfatp.getName());
            arraylist = WorkFlowStepUtil.getUserInTheContainerTeam(persistable, rolestr);
        }
        return arraylist;
    }

    public ComponentConfig buildComponentConfig(ComponentParams componentparams) throws WTException
    {
        TableConfig tableconfig = newTableConfig();
        tableconfig.setType(WTPrincipal.class.getName());
        tableconfig.setLabel(getMessage("SELECT_PARTICIPANTS"));
        tableconfig.setSelectable(true);
        tableconfig.setId("selectParticipantsTable");
        ColumnConfig columnconfig = newColumnConfig("name", true);
        columnconfig.setDataUtilityId("name");
        tableconfig.addComponent(columnconfig);
        ColumnConfig columnconfig2 = newColumnConfig("setPrincipalFullName", true);
        columnconfig2.setDataUtilityId("setPrincipalFullName");
        columnconfig2.setLabel(getMessage("FULL_NAME"));
        tableconfig.addComponent(columnconfig2);
        ColumnConfig columnconfig3 = newColumnConfig("wtprincipal_role", true);
        columnconfig3.setDataUtilityId("wtprincipal_role");
        columnconfig3.setLabel(getMessage("ROLE_BELONG"));
        tableconfig.addComponent(columnconfig3);
        tableconfig.setShowCount(true);
        tableconfig.setShowCustomViewLink(false);
        return tableconfig;
    }


    protected TableConfig newTableConfig()
    {
        return getComponentConfigFactory().newTableConfig();
    }

    protected ColumnConfig newColumnConfig(String s, boolean flag)
    {
        return getComponentConfigFactory().newColumnConfig(s, flag);
    }

    protected String getMessage(String s)
    {
        return messageSource.getMessage(s);
    }

}
